#include "SearchWindow.h"

SearchWindow::SearchWindow(QWidget *qw)  : QMainWindow(qw)
{
    this->setGeometry(qw->pos().x()+500,qw->pos().y()+150,150,150);
    this->setFixedSize(150,150);
    this->setWindowTitle(tr("Find"));
    this->setParent(qw);

    QLabel* lbl_search = new QLabel(this);
    lbl_search->setObjectName(QString::fromUtf8("lbl_search"));
    lbl_search->setGeometry(30, 5, 100, 20);
    lbl_search->setText(tr("Search for..."));

    led = new QLineEdit(this);
    led->setGeometry(30, 25, 95, 18);
    connect(led, SIGNAL(returnPressed()), this, SLOT(nextClicked()));

    ch_backwards = new QCheckBox(this);
    ch_backwards->setObjectName(QString::fromUtf8("ch_backwards"));
    ch_backwards->setGeometry(30, 45, 100, 18);
    ch_backwards->setText(tr("Backwards"));
    ch_backwards->setChecked(false);

    ch_caseSen = new QCheckBox(this);
    ch_caseSen->setObjectName(QString::fromUtf8("ch_caseSen"));
    ch_caseSen->setGeometry(30, 65, 100, 18);
    ch_caseSen->setText(tr("Case sensitive"));
    ch_caseSen->setChecked(false);

    ch_wholeWords = new QCheckBox(this);
    ch_wholeWords->setObjectName(QString::fromUtf8("ch_wholeWords"));
    ch_wholeWords->setGeometry(30, 85, 100, 18);
    ch_wholeWords->setText(tr("Whole words"));
    ch_wholeWords->setChecked(false);

    btn_ok = new QPushButton(this);
    btn_ok->setObjectName(QString::fromUtf8("btn_ok"));
    btn_ok->setGeometry(10, 105, 40, 20);
    btn_ok->setText("Ok");
    connect(btn_ok, SIGNAL(pressed()), this, SLOT(searchClicked()));

    btn_next = new QPushButton(this);
    btn_next->setObjectName(QString::fromUtf8("btn_next"));
    btn_next->setGeometry(55, 105, 40, 20);
    btn_next->setText(tr("Next"));
    connect(btn_next, SIGNAL(pressed()), this, SLOT(nextClicked()));

    QPushButton* btn_close = new QPushButton(this);
    btn_close->setObjectName(QString::fromUtf8("btn_close"));
    btn_close->setGeometry(100, 105, 40, 20);
    btn_close->setText(tr("Close"));
    connect(btn_close, SIGNAL(pressed()), this, SLOT(close()));

    this->show();
}

SearchWindow* SearchWindow::instance = NULL;

SearchWindow* SearchWindow::getInstance(QWidget *qw)
{
    if(!instance)
        instance = new SearchWindow(qw);

    return instance;
}

void SearchWindow::Destroy()
{
    if(instance)
    {
        instance->close();
        delete instance;
        instance = NULL;
    }
}

void SearchWindow::setEdit(PlainEdit *edit)
{
    ed = edit;
}

void SearchWindow::searchClicked()
{
    int curBlock = ed->textCursor().blockNumber();
    ++curBlock; //blockNumber starts with 0, but we need line 1 ;)

    ed->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);

    QFlags<QTextDocument::FindFlag> findFlags = 0;
    if(ch_backwards->isChecked())
    {
        findFlags = findFlags | QTextDocument::FindBackward;
        ed->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
    }

    if(ch_caseSen->isChecked())
        findFlags = findFlags | QTextDocument::FindCaseSensitively;

    if(ch_wholeWords->isChecked())
        findFlags = findFlags | QTextDocument::FindWholeWords;

    if(!ed->find(led->text(),findFlags))
        QMessageBox::about(this, tr("Find"), tr("Could not find \"%1\".").arg(led->text()));
    else
    {
        this->parentWidget()->raise(); //wont work for me :S
    }
}


void SearchWindow::nextClicked()
{
    if(ed->textCursor().atEnd())
        ed->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);

    QFlags<QTextDocument::FindFlag> findFlags = 0;
    if(ch_backwards->isChecked())
        findFlags = findFlags | QTextDocument::FindBackward;

    if(ch_caseSen->isChecked())
        findFlags = findFlags | QTextDocument::FindCaseSensitively;

    if(ch_wholeWords->isChecked())
        findFlags = findFlags | QTextDocument::FindWholeWords;

    if(!ed->find(led->text(),findFlags))
    {
        ed->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
        QMessageBox::about(this, tr("Find"), tr("Could not find \"%1\".").arg(led->text()));
    }
    else
    {
        //Set the mainwindow to the selected one
    }
}
